package morenoapp.components.common;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.border.LineBorder;
import morenoapp.components.help.MorenoHelp;
import morenoapp.util.MorenoSwing;
import morenoapp.util.MorenoUtil;

/**
 *
 * @author dmitry.mamonov
 */
public class ExpandablePanel extends JPanel {
    JToolBar header;
    LinkLabel caption;
    String captionText;
    JButton help;
    JComponent content = null;
    public ExpandablePanel(JComponent _content, String _captionText, String _helpId) {
        super(new BorderLayout());
        {
            this.captionText = _captionText;
            caption = new LinkLabel();
            caption.setName("caption");
            caption.setText(this.captionText);
            caption.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    content.setVisible(!content.isVisible());
                    if (content.isVisible()==false){
                        setPreferredSize(header.getPreferredSize());
                    } else {
                        Dimension pref = header.getPreferredSize();
                        pref.height+=content.getPreferredSize().height;
                        setPreferredSize(pref);
                    }
                    initLayout();
                    invalidate();
                    validate();
                    repaint();
                    //getParent().doLayout();
                }
            });
        }
        {
            help = MorenoHelp.createHelpButton(_helpId);
        }
        {
            header = new JToolBar("header", JToolBar.HORIZONTAL);
            BoxLayout layout = new BoxLayout(header, BoxLayout.LINE_AXIS);
            header.setLayout(layout);
            header.setFloatable(false);
            header.add(caption);
            header.add(Box.createHorizontalGlue());
            header.add(help);
        }
        {
            if (_content==null){
                throw new IllegalArgumentException("content must not be null");
            }
            this.content = _content;
        }
        setBorder(new LineBorder(Color.BLACK));
        initLayout();
    }

    protected void initLayout(){
        removeAll();
        add(header,BorderLayout.NORTH);
        if (content.isVisible()==true){
            add(content,BorderLayout.CENTER);
        }
    }

    public static void main(String[] args) {
        MorenoSwing.setupNumbus();
        JComponent c1 = new ExpandablePanel(new JTextArea("Hello\nmy\nWorld\n\n\n"),"Hello World","hello_world");
        JComponent c2 = new ExpandablePanel(new JTextArea("Hello\nmy\nWorld\n\n\n"),"Hello World","hello_world");

        JPanel p = MorenoUtil.createBoxLayoutPanel(BoxLayout.PAGE_AXIS);
        p.add(c1);
        p.add(c2);
        p.add(Box.createVerticalGlue());
        p.add(new JButton());

        MorenoSwing.showComponentInFrame(p);
    }
}
